package com.cjjd.c.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/30.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class Mylistview extends ListView {
    public Mylistview(Context context) {
        super(context);
    }

    public Mylistview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Mylistview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private float downX;
    private float downY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = ev.getY();
                downX = ev.getX();
                //Log.e("xyd", "按下坐标是：" + downX + ";" + downY);
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(ev.getX() - downX) > Math.abs(ev.getY() - downY)) {
                    setParentScrollAble(true);
                } else {
                    setParentScrollAble(false);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                setParentScrollAble(true);
                break;

        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = ev.getY();
                downX = ev.getX();
                //Log.e("xyd", "按下坐标是：" + downX + ";" + downY);
            case MotionEvent.ACTION_MOVE:
                //Log.e("xyd", "滑动坐标是：" + ev.getX() + ";" + ev.getY());
                if (Math.abs(ev.getX() - downX) > Math.abs(ev.getY() - downY)) {
                    //Log.e("xyd", "X方向滑动");
                    setParentScrollAble(true);
                } else if (isListViewReachBottomEdge() && ev.getY() - downY < 0) {
                    //Log.e("xyd", "Y方向向上滑到底了");
                    setParentScrollAble(true);
                }else if (isFirstViewReachTop() && ev.getY() - downY > 0) {
                    //Log.e("xyd", "Y方向向下滑动到顶了");
                    setParentScrollAble(true);
                } else {
                    //Log.e("xyd", "Y方向滑动");
                    setParentScrollAble(false);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                setParentScrollAble(true);
                break;

        }
        return super.onTouchEvent(ev);
    }

    public boolean isFirstViewReachTop() {
        boolean result = false;

        if (getFirstVisiblePosition() == 0){
            View firstView = getChildAt(0);
            //Log.e("xyd","listview 的顶部Y值是："+ firstView.getTop());
            result = firstView.getTop()==0;
        }

        return result;
    }

    public boolean isListViewReachBottomEdge() {
        boolean result = false;
        if (getLastVisiblePosition() == (getCount() - 1)) {
            final View bottomChildView = getChildAt(getLastVisiblePosition() - getFirstVisiblePosition());
            result = (getHeight() >= bottomChildView.getBottom());
        }
        return result;
    }


    // 设置父控件是否可以获取到触摸处理权限
    private void setParentScrollAble(boolean flag) {
        getParent().requestDisallowInterceptTouchEvent(!flag);
    }

}
